package com.kinon.ceshiaccount.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.GridView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.kinon.ceshiaccount.R;
import com.kinon.ceshiaccount.adapter.TotalPriceAdapter;
import com.kinon.ceshiaccount.bean.TotalPriceBean;
import com.kinon.ceshiaccount.bean.base.BaseBean;
import com.kinon.ceshiaccount.network.HttpManage;
import com.kinon.ceshiaccount.network.StrCallback;
import com.kinon.ceshiaccount.util.MyShowDialog;
import com.kinon.ceshiaccount.util.SPManage;
import com.kinon.ceshiaccount.util.ToastUtil;

import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by lhqq on 2017-12-21.
 */

public class TotalPriceActivity extends BaseActivity implements View.OnClickListener {

    private TextView tv_back;
    private TextView tv_totalOrder;
    private TextView tv_totalPrice;
    private GridView gv_total;

    ToastUtil mToast=new ToastUtil();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_totalprice_layout);
        initView();
    }

    private void initView() {
        tv_back= (TextView) findViewById(R.id.tv_back);
        tv_totalOrder= (TextView) findViewById(R.id.tv_totalOrder);
        tv_totalPrice= (TextView) findViewById(R.id.tv_totalPrice);
        gv_total= (GridView) findViewById(R.id.gv_total);

        tv_back.setOnClickListener(this);
        getTotalInfo();
    }

    private void getTotalInfo(){
        Call<String> call= HttpManage.getRequestApi()
                .getTotalData(SPManage.getInstance().getToken());
        MyShowDialog.showLoadingDialog(TotalPriceActivity.this,"数据加载中");
        call.enqueue(new StrCallback<String>() {
            @Override
            public void onSuccess(Response<String> response, String data) {
                MyShowDialog.closeLoadingDialog();
                Gson gson=new Gson();
                TotalPriceBean total=gson.fromJson(data,TotalPriceBean.class);
                tv_totalOrder.setText(String.valueOf(total.getAll_num()));
                tv_totalPrice.setText("￥"+String.valueOf(total.getAll_total())+"元");
                TotalPriceAdapter adapter=new TotalPriceAdapter
                        (total.getOther(),TotalPriceActivity.this);
                gv_total.setAdapter(adapter);
            }

            @Override
            public void onError(int code, String error) {
                MyShowDialog.closeLoadingDialog();
                mToast.Short(error);
            }
        });
    }



    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_back:
                finish();
            break;
        }
    }
}
